package com.luocj.muke.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.utils.Utils;

public class MyLineChartView extends View {

    private int widthDefault;
    private int heightDefault;
    private int marginDefault;
    private Paint mPaint;
    private Paint lablePaint;
    private Paint textPaint;
    private int textSize;


    public MyLineChartView(Context context) {
        this(context, null);
    }

    public MyLineChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        widthDefault = Utils.getScreenWidth(context);
        heightDefault = Utils.dip2px(context, 100);
        marginDefault = Utils.dip2px(context, 10);
        textSize = Utils.dip2px(context, 14);
        initPaint();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyLineChart);
        ta.getDimension(R.styleable.MyLineChart_line_margin, marginDefault);
        ta.getDimension(R.styleable.MyLineChart_text_size, textSize);
        ta.recycle();

    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        lablePaint = new Paint();
        lablePaint.setColor(Color.BLACK);
        lablePaint.setAntiAlias(true);
        lablePaint.setStyle(Paint.Style.STROKE);

        /**
         * 文字画笔
         */

        textPaint = new Paint();
        textPaint.setStrokeWidth(1);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(Utils.dip2px(getContext(),12));
        textPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = widthDefault;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = heightDefault;
        }

        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#436EEE"));
        canvas.translate(0, getBottom());

        drawYLable(canvas);
        drawYLine(canvas);
        drawXLine(canvas);
        drawXLable(canvas);

        drawDataLines(canvas);

    }

    /**
     * 连线
     * @param canvas
     */
    private void drawDataLines(Canvas canvas) {

    }

    /**
     * x 轴
     *
     * @param canvas
     */
    private void drawXLable(Canvas canvas) {
        int width = getWidth()-90;
        float subWidth = width / 3;
        for (int i = 0; i < 4; i++) {
            canvas.drawText(i+"", i *subWidth+20,-40,textPaint );
        }

    }

    private void drawXLine(Canvas canvas) {
        canvas.drawLine(10, -100, getRight(), -100, mPaint);
    }

    private void drawYLine(Canvas canvas) {
        canvas.drawLine(10, -100, 10, -getBottom(), mPaint);
    }

    private void drawYLable(Canvas canvas) {

    }
}
